package com.example.yunyuapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {


    private TextView tvMainLocation;
    private TextView tvMainPM25;
    private TextView tvMainCT;
    private TextView tvMainSO2;
    private TextView tvMainO3;
    private TextView tvMainCO;
    private TextView tvMainNO2;
    private TextView tvMainAQI;
    private TextView tvMainQuality;
    private RecyclerView tvMainTop;
    private ImageView igMainSearch;

    //设置RecyclerView适配器
    PM2_5RVAdapter pm2_5RVAdapter;

    OkHttpClient okHttpClient = new OkHttpClient();
    Gson gson = new Gson();

    //    static final String APPCODE = "60d25fe9abf44719a8947a8f85c66b68"; 次数用完
    static final String APPCODE = "ca224e2f11324dbe91458c481d4a608e";
    static final String URL = "http://ali-pm25.showapi.com/pm25-detail";
    static final String URLTOP = "http://ali-pm25.showapi.com/pm25-top";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        //搜索跳转页面
        igMainSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,SearchActivity.class);
                startActivity(intent);
            }
        });

        tvMainTop.setLayoutManager(new LinearLayoutManager(this));

        //主页城市空气数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                Request.Builder reqBuild = new Request.Builder();
                //APPCODE验证
                reqBuild.addHeader("Authorization", "APPCODE " + APPCODE);
                //城市空气数据链接
                HttpUrl.Builder urlBuilder = HttpUrl.parse(URL).newBuilder();
                urlBuilder.addQueryParameter("city", "佛山");
                reqBuild.url(urlBuilder.build());
                Request request = reqBuild.build();

                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        String result = response.body().string();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                PM2_5 pm2_5 = gson.fromJson(result, PM2_5.class);

                                String location = pm2_5.getShowapi_res_body().getPm().getArea();
                                String pm = pm2_5.getShowapi_res_body().getPm().getPm2_5();
                                String ct = pm2_5.getShowapi_res_body().getPm().getCt();
                                String so2 = pm2_5.getShowapi_res_body().getPm().getSo2();
                                String o3 = pm2_5.getShowapi_res_body().getPm().getO3();
                                String co = pm2_5.getShowapi_res_body().getPm().getCo();
                                String no2 = pm2_5.getShowapi_res_body().getPm().getNo2();
                                String aqi = pm2_5.getShowapi_res_body().getPm().getAqi();
                                String quality = pm2_5.getShowapi_res_body().getPm().getQuality();

                                tvMainLocation.setText(location);
                                tvMainPM25.setText("PM2.5：" + pm);
                                tvMainCT.setText("时间：" + ct);
                                tvMainSO2.setText("SO2：" + so2);
                                tvMainO3.setText("O3：" + o3);
                                tvMainCO.setText("CO：" + co);
                                tvMainNO2.setText("NO2：" + no2);
                                tvMainAQI.setText("空气指数：" + aqi);
                                tvMainQuality.setText("空气质量：" + quality);
//                                Log.i("TAG","msg"+result);
                            }
                        });
                    }

                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    }
                });
            }
        }).start();

        //城市空气排行
        new Thread(new Runnable() {
            @Override
            public void run() {
                Request.Builder reqBuildTop = new Request.Builder();
                reqBuildTop.addHeader("Authorization", "APPCODE " + APPCODE);
                HttpUrl.Builder urlBuilderTop = HttpUrl.parse(URLTOP).newBuilder();
                reqBuildTop.url(urlBuilderTop.build());
                Request requestTop = reqBuildTop.build();

                Call callTop = okHttpClient.newCall(requestTop);
                callTop.enqueue(new Callback() {
                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        String result = response.body().string();
                        PM2_5TOP pm2_5top = gson.fromJson(result, PM2_5TOP.class);
                        pm2_5RVAdapter = new PM2_5RVAdapter(pm2_5top);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tvMainTop.setAdapter(pm2_5RVAdapter);
                            }
                        });
                    }

                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {

                    }
                });
            }
        }).start();
    }

    private void initView() {
        tvMainLocation = (TextView) findViewById(R.id.tvMainLocation);
        tvMainPM25 = (TextView) findViewById(R.id.tvMainPM2_5);
        tvMainCT = (TextView) findViewById(R.id.tvMainCT);
        tvMainSO2 = (TextView) findViewById(R.id.tvMainSO2);
        tvMainO3 = (TextView) findViewById(R.id.tvMainO3);
        tvMainCO = (TextView) findViewById(R.id.tvMainCO);
        tvMainNO2 = (TextView) findViewById(R.id.tvMainNO2);
        tvMainAQI = (TextView) findViewById(R.id.tvMainAQI);
        tvMainQuality = (TextView) findViewById(R.id.tvMainQuality);
        tvMainTop = (RecyclerView) findViewById(R.id.tvMainTop);
        igMainSearch = (ImageView) findViewById(R.id.igMainSearch);
    }
}